--
-- Author: shunguo.chen
-- Date: 2016-10-19 17:56:54
--
-- RewardLoginRender

local IconRender = require("app.widget.renders.IconRender")

local ClassRef = app.mvc.createRenderClass(...)

--data = RewardLoginVo
function ClassRef.create(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RewardLoginNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self:setContentSize(self._ui.date:getContentSize())

	self._ui.Button:addClickEventListener(function()
			executeFunc(self._onBtnClick, "scroll")
		end)
	self._ui.btn_enter:addClickEventListener(function()
			executeFunc(self._onBtnClick, "click")
		end)

	self._items = {}
	for i=1,4 do
		local render = IconRender.create(self._ui["icon_" .. i])
		render:setTipEnable(true, {x=1/20,y=2/5})
		render:setClickHandler(function()
				executeFunc(self._onBtnClick, "scroll")
			end)
		self._items[i] = render
	end

	self._ui["date/num"]:setVisible(true)

	self:refresh()
end

function ClassRef:refresh()
	if self._data then
		self:refreshTime()

		-- self._ui["date/name"]:setString(string.format(L("text_11400029"), self._data.day))
		self._ui["date/num"]:setString(self._data.day)

		self._ui.btn_enter:setVisible(false)
		self._ui.icon_y:setVisible(false)
		self._ui.icon_n:setVisible(false)
		if self._data.day < self._data.today then
			self._ui.icon_y:setVisible(true)
		elseif self._data.day == self._data.today then
			self._ui.icon_y:setVisible(self._data.isGot)
			self._ui.btn_enter:setVisible(not self._data.isGot)
		else
			self._ui.icon_n:setVisible(true)
		end

		for i,item in ipairs(self._items) do
			local v = self._data.rewards[i]
			item:setVisible(v ~= nil)
			if v then
				item:setData(v)
			end
		end
	end
end

function ClassRef:refreshTime()
	if self._delayAct then
		self:stopAction(self._delayAct)
		self._delayAct = nil
	end

	if self._data and self._data:isTomorrow() and self._data.isShowTime then
		local param = TimeUtil.secondsToStr(self._data:getNextTime(), 3)
		self._ui.time_0:setString(string.format(L("text_11400028"), unpack(param)))

		self._delayAct = self:performWithDelay(function()
				self._delayAct = nil
				self:refreshTime()
			end, 1)
	else
		self._ui.time_0:setString("")
	end
end

function ClassRef:onBtnClick(cb)
	self._onBtnClick = cb
end

return ClassRef
